package com.example.cnotebook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.cnotebook.pojo.UserInfo;
import com.example.cnotebook.util.UserConstant;

/**
 * 用户中心
 */
public class UserActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivBack;
    private LinearLayout itemAccount;
    private TextView tvAccount;
    private LinearLayout itemNickname;
    private TextView tvNickname;
    private LinearLayout itemUpdatePwd;
    private Button btnLogout;

    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        btnLogout = findViewById(R.id.btn_logout);
        ivBack = findViewById(R.id.iv_back);
        itemAccount = findViewById(R.id.item_account);
        tvAccount = findViewById(R.id.tv_account);
        itemNickname = findViewById(R.id.item_nickname);
        tvNickname = findViewById(R.id.tv_nickname);
        itemUpdatePwd = findViewById(R.id.item_updatePwd);
        btnLogout = findViewById(R.id.btn_logout);

        btnLogout.setOnClickListener(this);
        ivBack.setOnClickListener(this);
        itemNickname.setOnClickListener(this);
        itemUpdatePwd.setOnClickListener(this);

        sp = getSharedPreferences("UserInfo", MODE_PRIVATE);
        initData();
    }

    private void initData() {
        UserInfo userInfo = UserConstant.userInfo;
        tvNickname.setText(userInfo.getNickname());
        tvAccount.setText(userInfo.getAccount());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_updatePwd:
                startActivity(new Intent(this, UpdatePasswordActivity.class));
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_logout:
                sp.edit().putBoolean("login", false).apply();
                startActivity(new Intent(this, LoginActivity.class));
                finish();
                break;
            case R.id.item_nickname:
                break;
        }
    }
}
